package com.admin.education.services.Implements;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.designations.DesignationsTransfer;
import com.admin.education.dataTransferObject.grades.GradesTransfer;
import com.admin.education.dataTransferObject.grades.Mapper.GradesMapper;
import com.admin.education.dataTransferObject.grades.Request.CreateGradeRequest;
import com.admin.education.dataTransferObject.grades.Request.UpdateGradeRequest;
import com.admin.education.models.Designations;
import com.admin.education.models.Grades;
import com.admin.education.models.QGrades;
import com.admin.education.repositories.GradesRepository;
import com.admin.education.services.Interfaces.IGradesService;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class GradesService implements IGradesService {
    @Autowired
    private GradesRepository gradesRepository;
    @Autowired
    private GradesMapper gradesMapper;
    private QGrades qGrades;

    public GradesService() {
        qGrades = QGrades.grades;
    }

    @Override
    public ServicesExcuteResult<List<GradesTransfer>> queryGrades() {
        ServicesExcuteResult<List<GradesTransfer>> servicesExcuteResult = new ServicesExcuteResult<>();

        JPAQuery<Grades> gradesJPAQuery = this.gradesRepository.selectFrom(qGrades);

        servicesExcuteResult.setData(gradesMapper.mapFrom(gradesJPAQuery.fetch()));
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult createGrades(@Valid CreateGradeRequest createGradeRequest) {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        Grades grades = this.gradesMapper.mapFrom(createGradeRequest);

        grades.setCreateTime(new Date());

        this.gradesRepository.persist(grades);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult updateGrades(long id, @Valid UpdateGradeRequest updateGradeRequest) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Grades> gradesJPAQuery = this.gradesRepository.selectFrom(qGrades).where(qGrades.id.eq(id));
        if (0 != gradesJPAQuery.fetchCount()) {
            Grades grades = gradesJPAQuery.fetchFirst();

            grades = this.gradesMapper.mapFrom(updateGradeRequest, grades);

            this.gradesRepository.merge(grades);
            servicesExcuteResult.setSuccess(true);
        } else {
            throw new ResourceNotFoundException("年级不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult deleteGrades(long id) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Grades> gradesJPAQuery = this.gradesRepository.selectFrom(qGrades).where(qGrades.id.eq(id));
        if (0 != gradesJPAQuery.fetchCount()) {

            Grades grades = gradesJPAQuery.fetchFirst();
            if (0 == grades.getClasses().size()) {
                this.gradesRepository.remove(grades);
                servicesExcuteResult.setSuccess(true);
            } else {
                servicesExcuteResult.setErrorMessage("不能删除该年级");
            }
        } else

        {
            throw new ResourceNotFoundException("年级不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<PageResponse<GradesTransfer>> queryGrades(@Valid PageRequest pageRequest) throws ResourceNotFoundException {
        ServicesExcuteResult<PageResponse<GradesTransfer>> servicesExcuteResult = new ServicesExcuteResult<>();

        JPAQuery<Grades> gradesJPAQuery = this.gradesRepository.selectFrom(qGrades);

        long totalCount = gradesJPAQuery.fetchCount();
        gradesJPAQuery = gradesJPAQuery.orderBy(qGrades.createTime.desc())
                .offset((pageRequest.getPageIndex() - 1) * pageRequest.getPageSize()).limit(pageRequest.getPageSize());

        List<Grades> grades = gradesJPAQuery.fetch();

        List<GradesTransfer> gradesTransfers = this.gradesMapper.mapFrom(grades);
        PageResponse<GradesTransfer> gradesTransferPageResponse = new PageResponse<>(pageRequest.getPageSize(), pageRequest.getPageIndex(), (int) totalCount, gradesTransfers);

        servicesExcuteResult.setData(gradesTransferPageResponse);
        servicesExcuteResult.setSuccess(true);

        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<GradesTransfer> queryGradesById(long id) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Grades> gradesJPAQuery = this.gradesRepository.selectFrom(qGrades).where(qGrades.id.eq(id));
        if (0 != gradesJPAQuery.fetchCount()) {
            Grades grades = gradesJPAQuery.fetchFirst();

            servicesExcuteResult.setData(this.gradesMapper.mapFrom(grades));
            servicesExcuteResult.setSuccess(true);
        } else {
            throw new ResourceNotFoundException("年级不存在");
        }
        return servicesExcuteResult;
    }
}
